/*
 ------------------------------------------------------------------------------
	winmad++ -	a test program for using SDL and mad library on Windows
				with object oriented UI style
				with low level mad (seek)

	Copyright (C) 2011 Pierre Veber
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software Foundation,
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
------------------------------------------------------------------------------
*/

#include "precomp.h"

#pragma once

#define THIS_FILTER	TRACE_STYLE

#define FREE(p)				if (p!=NULL) { free(p); }
#define IS_STYLE(p,	s)		( strcmp(p, s) == 0 )

int __int_val(char* pVal, int* piVal)
{
	/* numeric value has 'px' suffix */
	char* pSuf = strstr(pVal, "px");
	if (pSuf != NULL)
	{
		*pSuf = '\0';
		*piVal = atoi(pVal);
		*pSuf = 'p';
		return 1;
	}
	return 0;
}

int __rect_val(char* pVal, style_rect_t* prVal)
{
	int iVal[4];
	char* pLast = pVal;
	char* p;
	int count = 0;

	/* must decompose with spaces */
	do
	{
		/* remove preceding spaces */
		while ( *pLast == ' ' ) { pLast++; }

		/* search for space */
		p = strchr(pLast, ' ');
		if (p != NULL)
		{
			char* pPixels = _strndup(pLast, p-pLast);

			/* numeric value has 'px' suffix */
			char* pSuf = strstr(pPixels, "px");
			if (pSuf != NULL)
			{
				*pSuf = '\0';
				iVal[count] = atoi(pVal);
				count++;
			}
		}
		pLast = p + 1;
	
	} while ( (*pLast != '\0') && (count < 4) );

	switch (count)
	{
		case 1:
			prVal->bottom = prVal->left = prVal->top = prVal->right = iVal[0];
			break;
		default:
			break;
	}
			
	return count;			
}

int __url_val(char* pVal, char** ppVal)
{
	if (strstr(pVal, "url('") != NULL)
	{
		char* pUrl = pVal + 5;
		char* p = strstr(pUrl, "')");
		if (p != NULL)
		{
			*ppVal = _strndup(pUrl, p-pUrl);
			return 1;
		}
	}
	return 0;
}

CStyle::CStyle()
{
	this->lpszBackground = NULL;
	this->margin = make_rect_t(2, 2, 2, 2);
	this->padding = make_rect_t(2, 2, 2, 2);
	this->width = 100;
	this->height = 100;
	this->top = 0;
	this->left = 0;
	this->fontsize = 12;
	this->display = STYLE_DISPLAY_BLOCK;
}

CStyle::~CStyle ()
{
	FREE(this->lpszBackground);
}
	
void CStyle::parse(char* lpszStyle)
{
	char* lpszType;
	char* lpszValue;

	char* pOne = lpszStyle;
	while (*pOne != '\0')
	{
		char* pLast = strchr(pOne, ';');
		if (pLast == NULL)
		{
			TRACE("CStyle::parse() - terminating ';' not found in [%s].\n", pOne);
			throw "CStyle::parse() - terminating ';' not found.";
		}
		char* lpszFirstStyle = _strndup(pOne, pLast-pOne);
		char* pSep = strchr(lpszFirstStyle, ':');
		if (pSep == NULL)
		{
			TRACE("CStyle::parse() - separator ':' not found in [%s].\n", lpszFirstStyle);
			throw "CStyle::parse() - separator ':' not found.";
		}
		lpszType = _strndup(lpszFirstStyle, pSep-lpszFirstStyle);
		lpszValue = _strdup(pSep + 1);

		_pack(lpszType);

		if ( IS_STYLE(lpszType, "margin") )
		{
			__rect_val(lpszValue, &(this->margin));
		}
		if ( IS_STYLE(lpszType, "padding") )
		{
			__rect_val(lpszValue, &(this->padding));
		}
		if ( IS_STYLE(lpszType, "background-image") )
		{
			_pack(lpszValue);
			__url_val(lpszValue, &(this->lpszBackground));
		}
		if ( IS_STYLE(lpszType, "width") )
		{
			_pack(lpszValue);
			__int_val(lpszValue, &(this->width));
		}
		if ( IS_STYLE(lpszType, "height") )
		{
			_pack(lpszValue);
			__int_val(lpszValue, &(this->height));
		}
		if ( IS_STYLE(lpszType, "left") )
		{
			_pack(lpszValue);
			__int_val(lpszValue, &(this->left));
		}
		if ( IS_STYLE(lpszType, "top") )
		{
			_pack(lpszValue);
			__int_val(lpszValue, &(this->top));
		}
		
		FREE(lpszFirstStyle);
		FREE(lpszType);
		FREE(lpszValue);

		pOne = pLast + 1;
	}
}




